package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.SaveGrouponBean;
import com.ymatou.iapi.productmanager.parameter.StopGrouponBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.StopGrouponCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import org.apache.http.client.ClientProtocolException;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
*停止拼团
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_StopGroupon{
	private static StopGrouponBean stopgrouponBean;
	private static StopGrouponCall stopgrouponCall;
	private static YmtProductsIWapper YmtProductsIWapper;
	/**
	 * 非优选
	 */
	private static Integer sellerId2 = Integer.valueOf(EnvSetup.getData("autotest_seller1"));
	/**
	 * 优选
	 */
	private static Integer sellerId = 3383;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("停止拼团");
	}

	@Before
	public void caseUp() {
		stopgrouponBean=new StopGrouponBean();
		stopgrouponCall=new StopGrouponCall();
		YmtProductsIWapper = new YmtProductsIWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(stopgrouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void setStock(AddSellerProductRequest addpro, Integer catalogNum) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setStock(catalogNum);
		});
	}

	private void setPrice(AddSellerProductRequest addpro, Double price) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setNewPrice(price);
			catalog.setMarketPrice(price);
			catalog.setVipPrice(price);
			catalog.setPrice(price);
		});
	}

	private void verifyMongoCurMq(Integer getGrouponProductId, String pid, String code) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(1000);
		List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(pid, null, 1);
		String version = gp.get(0).getVersion().toString();
		MessageListBean MessageListBean2 = MqQueryCallService.initPMongoCrud(pid);
		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		Logger.debug("joa2:"+joa2);
		Logger.verifyEquals(code, joa2.get(0).get("ActionType"), "验证ActionType");
		Logger.verifyEquals(getGrouponProductId, joa2.get(0).get("ActivityId"), "验证ActivityId");
	}

	private void verifyGroupMq(Integer getGrouponProductId, String pid,String qcode) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(1000);
		MessageListBean MessageListBean = MqQueryCallService.initGroupon(pid, qcode);
		List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
		Logger.debug(joa);
		// {"EndTime":"4000-01-31
		// 23:59:00","GroupExpiredHours":6,"GroupMemberCount":2,"GrouponProductId":33,"ProductId":"p1158472"}
		Logger.comment("验证砍价团总线消息");
		Logger.verifyEquals(getGrouponProductId, joa.get(0).get("GrouponProductId"), "验证GrouponProductId");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StopGroupon_001() {
		Logger.start(true,"拼团商品停止拼团，成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,productId,80d);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			int groupProductId = YmtProductsIWapper.selectGrouponProducts(saveGrouponBean.getProductId(), null, 0).get(0).getGrouponProductId();
			stopgrouponBean.setGroupProductId(groupProductId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();			
			Logger.verifyEquals("200",stopgrouponCall.getString("Code"),"验证Code");
			int status = YmtProductsIWapper.selectGrouponProducts(saveGrouponBean.getProductId(), null, 1).get(0).getStatus();
			Logger.verifyEquals(1,status,"验证Status状态");
			List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(productId, null, 1);

			//momgo库，GrouponProducts没有数据，未同步到mongo
//			verifyMongoCurMq(gp.get(0).getGrouponProductId(),productId,"StopGroupon");
//			verifyGroupMq(gp.get(0).getGrouponProductId(),productId,"stop_groupon_god");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StopGroupon_002() {
		Logger.start(true,"创建拼团商品，开团，停团，再开团，再停团");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,productId,80d);
			//开团
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			int groupProductId = YmtProductsIWapper.selectGrouponProducts(saveGrouponBean.getProductId(), null, 0).get(0).getGrouponProductId();
			//停团
			stopgrouponBean.setGroupProductId(groupProductId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();
			Logger.verifyEquals("200",stopgrouponCall.getString("Code"),"验证Code");
			int status = YmtProductsIWapper.selectGrouponProducts(saveGrouponBean.getProductId(), null, 1).get(0).getStatus();
			Logger.verifyEquals(1,status,"验证Status状态");
			List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(productId, null, 1);

//			verifyMongoCurMq(gp.get(0).getGrouponProductId(),productId,"StopGroupon");
//			verifyGroupMq(gp.get(0).getGrouponProductId(),productId,"stop_groupon");
			//开团
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			//停团
			List<GrouponProducts> grouponProducts = YmtProductsIWapper.selectGrouponProducts(saveGrouponBean.getProductId(), null, 0);
			groupProductId = grouponProducts.get(0).getGrouponProductId();
			stopgrouponBean.setGroupProductId(groupProductId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();
			Logger.verifyEquals("200",stopgrouponCall.getString("Code"),"验证Code");
			List<GrouponProducts> gp1 = YmtProductsIWapper.selectGrouponProducts(productId, Integer.parseInt(grouponProducts.get(0).getVersion().toString()), 1);
			status = gp1.get(0).getStatus();
			Logger.verifyEquals(1,status,"验证Status状态");
//			verifyMongoCurMq(gp1.get(0).getGrouponProductId(),productId,"StopGroupon");
//			verifyGroupMq(gp1.get(0).getGrouponProductId(),productId,"stop_groupon_god");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}


}